package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.Gift;
import cn.edu.gcu.giftems.pojos.Order;
import cn.edu.gcu.giftems.pojos.OrderItem;
import cn.edu.gcu.giftems.service.GiftService;
import cn.edu.gcu.giftems.service.OrderItemService;
import cn.edu.gcu.giftems.service.OrderService;
import cn.edu.gcu.giftems.utils.PageUtils;
import cn.edu.gcu.giftems.utils.export.OrderItemExportUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class OrderItemAction extends ActionSupport implements ModelDriven<OrderItem> {
    
    private OrderItemService orderItemService =
            (OrderItemService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("orderItemService");
    private OrderService orderService =
            (OrderService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("orderService");
    private GiftService giftService =
            (GiftService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("giftService");
    private OrderItem orderItem = new OrderItem();
    private List<Order> olist;
    private List<Gift> glist;

    @Override
    public OrderItem getModel() {
        return orderItem;
    }

    public void setOrderItem(OrderItem orderItem) {
        this.orderItem = orderItem;
    }

    public void setOrderItemService(OrderItemService orderItemService) {
        this.orderItemService = orderItemService;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public void setGiftService(GiftService giftService) {
        this.giftService = giftService;
    }

    public List<Order> getOlist() {
        return olist;
    }

    public void setOlist(List<Order> olist) {
        this.olist = olist;
    }

    public List<Gift> getGlist() {
        return glist;
    }

    public void setGlist(List<Gift> glist) {
        this.glist = glist;
    }

    /**
     * 分页查询订单单项的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<OrderItem> pageUtils = orderItemService.findByPage(currPage);
        // ActionContext.getContext().getValueStack().push(pageUtils);
        pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        olist = orderService.findAll();
        glist = giftService.findAll();
        return "success";
    }

    /**
     * 添加订单单项的执行方法
     */
    public String save(){
        orderItemService.save(orderItem);
        return this.findByPage();
    }

    /**
     * 更新订单单项信息的执行方法
     */
    public String update(){
        if (orderItem.getId() > 0)
            orderItemService.update(orderItem);
        return this.findByPage();
    }
    /**
     * 根据订单单项id删除订单单项
     */
    public String delete(){
        orderItem = orderItemService.findById(orderItem.getId());
        if (orderItem != null)
            orderItemService.delete(orderItem);
        return this.findByPage();
    }

    public void export()
    {
        OrderItemExportUtils Export=new OrderItemExportUtils();
        try {
            Export.export(orderItemService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String rank() {
        List<OrderItem[]> list = orderItemService.rank();
        ActionContext.getContext().put("list", list);
        return "index";
    }
}
